Rust Amplify Library
Amplifying Rust language capabilities: multiple generic trait implementations,
type wrappers, derive macros. Tiny library with zero non-optional dependencies.
Able to work as no_std
.
Minimum supported rust compiler version (MSRV): 1.75.0; rust edition 2021.
Main features
Generics
Library proposes generic implementation strategies, which allow multiple generic trait implementations.
Implementing trait for a generic type ("blanket implementation") more than once
(applies both for local and foreign traits) - or implement foreign trait for a
concrete type where there is some blanket implementation in the upstream. The
solution is to use special pattern by @Kixunil. I use it widely and have a
special helper type in src/strategy.rs
src/strategy.rs module.
With that helper type you can write the following code, which will provide you
with efficiently multiple blanket implementations of some trait SampleTrait
:
// Define strategies, one per specific implementation that you need,
// either blanket or concrete
;
;
;
// Define a single marker type
// Do a single blanket implementation using Holder and Strategy marker trait
// Do this type of implementation for each of the strategies
# ;
// Finally, apply specific implementation strategy to a concrete type
// (or do it in a blanket generic way) as a marker:
Derive macros
- Display
- From
- Error
- Getters
- AsAny
- Wrapper
A sample of what can be done with the macros:
More information is given in amplify_derive
crate README.
Macros
none!
as an alias forDefault::default()
on collection types and types for which semantics makes it sensible to emphasize that the operation initializes empty structure.s!
for fast&str
->String
conversions- Collection-generating macros:
map!
&bmap!
for a rappidHashMap
andBTreeMap
creationset!
&bset!
for a rappidHashSet
andBTreeSet
creationlist!
forLinkedList
Wapper type
Wrapper trait helps in creating wrapped rust newtypes, Wrapped types are used for allowing implemeting foreign traits to foreign types: https://doc.rust-lang.org/stable/rust-by-example/generics/new_types.html
Trait defines convenient methods for accessing inner data, construct and deconstruct newtype. It also serves as a marker trait for newtypes.
The trait works well with #[derive(Wrapper)]
from amplify_derive
crate
Build
cargo build --all
cargo test
As a reminder, minimum supported rust compiler version (MSRV) is 1.36.0, so it
can be build with either nightly, dev, stable or 1.36+ version of the rust
compiler. Use rustup
for getting the proper version, or add +toolchain
parameter to both cargo build
and cargo test
commands.
Benchmark
RUSTFLAGS="--cfg bench" cargo bench